package com.andengine.lwtemplate;

import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;

import com.andengine.lwtemplate.LiveWallpaper.WEngine;

public class MainThread extends Thread {
	private boolean run;
	private SurfaceHolder mSurfaceHolder;
	private WEngine panel;
	private boolean m_initialized;
	
	public MainThread(SurfaceHolder surfaceHolder, WEngine panel){
		this.mSurfaceHolder = surfaceHolder;
		this.run = false;
		this.panel = panel;
		
	}
	
	@Override
    public void run() {
		Log.d("LOL", "RUN!");
        Canvas c;
        while (this.run) {
            c = null;
            try 
            {
            	
	                c = this.mSurfaceHolder.lockCanvas(null);
	                synchronized (this.mSurfaceHolder) 
	                {
	                	Log.d("LOL", "DRAWWW!");
                		panel.drawFrame(c);
                		panel.update();
	                }
            	
            } finally 
            {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) 
                {
                	this.mSurfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
        
	}
	
	public void setRunning(){
		this.run = true;
	}
}
